<?php

namespace backend\models\download;

use Yii;
use yii\data\Pagination;
/**
 * This is the model class for table "edu_soft_course".
 *
 * @property integer $soft_id
 * @property integer $course_id
 */
class Softlog extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_log}}';
    }

    
    
    /**
     *软件日志 查询数据生成列表
     */
    public function getLogs($variable = NULL) {
        $var_where = $this->getLogwhere($variable);
        $time = $var_where['log_addtime']?$var_where['log_addtime']:['>','log_addtime',0];
        unset($var_where['log_addtime']);
        $pagination = new Pagination([
            'defaultPageSize' =>20,
            'totalCount' => (new \yii\db\Query())
                            ->select(['edu_user_log.log_id'])
                            ->from('edu_user_log')
                            ->innerjoin('edu_user','edu_user_log.user_id=edu_user.user_id')
                            ->where($var_where)
                            ->andwhere($time)
                            ->count()
        ]);

        $info = (new \yii\db\Query())
                ->select(['log_id','log_addtime','log_info','ip_address','user_name','user_email'])
                ->from('edu_user_log')
                ->innerjoin('edu_user','edu_user_log.user_id=edu_user.user_id')
                ->where($var_where)
                ->andwhere($time)
                ->offset($pagination->offset)
                ->limit($pagination->limit)
                ->all();
        
        return ['info'=>$info, 'paging'=>$pagination];
    }
    
    /**
     * 软件日志 条件拼接
     */
    private function getLogwhere($variable = NULL) {
       
        $session = Yii::$app->session;
        $session->open();
        
        if(Yii::$app->request->get('remove_where')==1){
            $session->remove('log_usertype');
            $session->remove('log_username');
            $session->remove('log_startdatetime');
            $session->remove('log_enddatetime');
        }
        
            $variable['usertype'] = !empty($variable['usertype'])?$variable['usertype']:$session->get('log_usertype');
            $variable['username'] = !empty($variable['username'])?$variable['username']:$session->get('log_username');
            $variable['startdatetime'] = !empty($variable['startdatetime'])?$variable['startdatetime']:$session->get('log_startdatetime');
            $variable['enddatetime'] = !empty($variable['enddatetime'])?$variable['enddatetime']:$session->get('log_enddatetime');
        
        if(!empty($variable['usertype'])&&!empty($variable['username'])) {
            $data[$variable['usertype']] = $variable['username'];
            $session->set('log_usertype',$variable['usertype']);
            $session->set('log_username',$variable['username']);
        }
        
        if(!empty($variable['startdatetime'])&&!empty($variable['enddatetime'])) {            
            $data['log_addtime'] = ['between','log_addtime',strtotime($variable['startdatetime']),strtotime($variable['enddatetime'])];
            $session->set('log_startdatetime',$variable['startdatetime']);
            $session->set('log_enddatetime',$variable['enddatetime']);
        }else if(!empty($variable['startdatetime'])) {
            $data['log_addtime'] = ['>','log_addtime',strtotime($variable['startdatetime'])];
            $session->set('log_startdatetime',$variable['startdatetime']);
        }else if(!empty($variable['enddatetime'])) {
            $data['log_addtime'] = ['<','log_addtime',strtotime($variable['enddatetime'])];
            $session->set('log_enddatetime',$variable['enddatetime']);
        }
       
            $data['log_type'] = 2;
        return $data;        
    }
    
    /**
     * 软件日志 条件拼接
    
    private function getLogwhere($variable = NULL) {
        
        $cookies_r = Yii::$app->request->cookies;
		
        if($cookies_r->has('logwhere')) {
            $cooks = $cookies_r->getValue('logwhere','en');
            //print_r($cooks);
            $variable['usertype'] = !empty($variable['usertype'])?$variable['usertype']:$cooks->value['usertype'];
            $variable['username'] = !empty($variable['username'])?$variable['username']:$cooks->value['username'];
            $variable['startdatetime'] = !empty($variable['startdatetime'])?$variable['startdatetime']:$cooks->value['startdatetime'];
            $variable['enddatetime'] = !empty($variable['enddatetime'])?$variable['enddatetime']:$cooks->value['enddatetime'];
            print_r($variable);
       }
        
        if(!empty($variable['usertype'])&&!empty($variable['username'])) {
            $data[$variable['usertype']] = $variable['username'];
        }
        
        if(!empty($variable['startdatetime'])&&!empty($variable['enddatetime'])) {            
            $data['log_addtime'] = ['between','log_addtime',strtotime($variable['startdatetime']),strtotime($variable['enddatetime'])];
        }else if(!empty($variable['startdatetime'])) {
            $data['log_addtime'] = ['>','log_addtime',strtotime($variable['startdatetime'])];
        }else if(!empty($variable['enddatetime'])) {
            $data['log_addtime'] = ['<','log_addtime',strtotime($variable['enddatetime'])];
        }
        
        //发送Cookie
        if($variable['is_add']==1) {
            $cookies = Yii::$app->response->cookies;
            $cookies->add(new \yii\web\Cookie([
                'name' => 'logwhere',
                'value' => [
                    'username' => $variable['username'],
                    'usertype' => $variable['usertype'],
                    'startdatetime'=> $variable['startdatetime'],
                    'enddatetime'=> $variable['enddatetime'],
                ]
            ])); 
        }
            $data['log_type'] = 2;
        return ['where' => $data, 'variable' => $variable];        
    } */
    

    
}
